<?php

namespace App\Http\Controllers\Admin\System;

use App\Http\Requests\StoreUser;
use App\Http\Requests\UpdateUser;
use App\Models\System\Role;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    public function __construct()
    {
        view()->share([
            '_system' => 'am-in',
            '_user' => 'am-active',

            'roles' => Role::all()
        ]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $where = function ($query) use ($request) {
            if ($request->has('keyword') and $request->keyword != '') {
                $search = "%" . $request->keyword . "%";
                $query->where('name', 'like', $search);
            }
        };

        $users = User::with('roles')->where($where)->paginate(9);
        return view('admin.system.user.index', compact('users'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.system.user.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreUser $request)
    {
        $user = User::create([
            'name' => $request->name,
            'email' => $request->email,
            'mobile' => $request->mobile,
            'password' => bcrypt($request->password),
        ]);
        $user->roles()->sync($request->role_id);
        return redirect(route('system.user.index'))->with('success', '新增用户成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $user = User::with('roles')->find($id);
        $user_roles = $user->roles->pluck('id');
        return view('admin.system.user.edit', compact('user', 'user_roles'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateUser $request, $id)
    {
        $user = User::find($id);

        if ($request->has('password') && $request->password != '') {
            if (!\Hash::check($request->old_password, $user->password)) {
                return back()->with('error', '原始密码错误~');
            }
//            $user->password = bcrypt($request->password);
        }

        $user->where('id', $id)->update([
            'name' => $request->name,
            'email' => $request->email,
            'mobile' => $request->mobile,
            'password' => bcrypt($request->password)
        ]);

        //第二种方法
//        $user->name = $request->name;
//        $user->mobile = $request->mobile;
//        $user->email = $request->email;
//        $user->save();

        $user->roles()->sync($request->role_id);
        return redirect(route('system.user.index'))->with('success', '编辑用户成功！');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        User::destroy($id);
    }

    /**
     * 改变状态
     */
    public function change(Request $request)
    {
        $user = User::find($request->id);
        $attr = $request->attr;
        $user->$attr = !$user->$attr;
        $user->save();
    }
}
